//
//  LBServiceManager.m
//  LBFoundation
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBServiceManager.h"

#define LBSerivceRegisterDelegate(protocolName, delegate) \
    if ([delegate conformsToProtocol:@protocol(protocolName)]) { \
        LBServiceManager *mananger = [LBServiceManager manager]; \
        [mananger.delegates setObject:delegate forKey:@#protocolName]; \
    }

#define BeginLBSerivceDelegateInstance(protocolName, method) \
    id<protocolName> delegate = [[LBServiceManager manager].delegates objectForKey:@#protocolName];  \
    if ([delegate conformsToProtocol:@protocol(protocolName)] \
        && [delegate respondsToSelector:@selector(method)]) {
#define EndLBSerivceDelegateInstance(defaultValue) } else { return defaultValue; }

@interface LBServiceManager ()

@property (nonatomic, strong) NSMutableDictionary<NSString *, id> *delegates;

@end

@implementation LBServiceManager

+ (void)registerLoginService:(id<LBLoginServiceDelegate>)delegate {
    LBSerivceRegisterDelegate(LBLoginServiceDelegate, delegate)
}

#if !(TARGET_OS_OSX)
+ (void)registerWebViewService:(id<LBWebViewServiceDelegate>)delegate {
    LBSerivceRegisterDelegate(LBWebViewServiceDelegate, delegate)
}
+ (void)registerReactService:(id<LBReactServiceDelegate>)delegate {
    LBSerivceRegisterDelegate(LBReactServiceDelegate, delegate)
}
#endif

+ (instancetype)manager {
    static LBServiceManager *_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

- (void)setDelegate:(id)delegate forKey:(NSString *)key {
    [self.delegates setValue:delegate forKey:key];
}

- (NSMutableDictionary<NSString *, id> *)delegates {
    if (_delegates) {
        return _delegates;
    }
    _delegates = [@{} mutableCopy];
    return _delegates;
}

#pragma mark - LBLoginServiceDelegate
+ (Class)loginControllerClass {
    BeginLBSerivceDelegateInstance(LBLoginServiceDelegate, loginControllerClass)
    return [delegate loginControllerClass];
    EndLBSerivceDelegateInstance(nil)
}

+ (BOOL)isLogin {
    BeginLBSerivceDelegateInstance(LBLoginServiceDelegate, isLogin)
    return [delegate isLogin];
    EndLBSerivceDelegateInstance(NO)
}

+ (void)login:(void(^)(void))completeHandler {
    BeginLBSerivceDelegateInstance(LBLoginServiceDelegate, login:)
    return [delegate login:completeHandler];
    EndLBSerivceDelegateInstance()
}

#if !(TARGET_OS_OSX)
#pragma mark - LBWebViewServiceDelegate
+ (UIViewController *)webController:(NSString *)URLString
                           userInfo:(NSDictionary *)userInfo {
    BeginLBSerivceDelegateInstance(LBWebViewServiceDelegate, webController:userInfo:)
    return [delegate webController:URLString
                          userInfo:userInfo];
    EndLBSerivceDelegateInstance(nil)
}

#pragma mark - LBReactServiceDelegate
+ (UIViewController *)reactController:(NSString *)URLString
                            component:(NSString *)component
                             userInfo:(NSDictionary *)userInfo {
    BeginLBSerivceDelegateInstance(LBReactServiceDelegate, reactController:component:userInfo:)
    return [delegate reactController:URLString
                           component:component
                            userInfo:userInfo];
    EndLBSerivceDelegateInstance(nil)
}

+(NSDictionary *)accountUserInfo:(BOOL)isSave saveData:(NSDictionary *)userData
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *keyStr = @"UserAccoutInfoKey_AifQXz2YWGvomql6nFs7w";
    if (isSave) {
        if (userData && [userData isKindOfClass:[NSDictionary class]]) {
            NSString *isguest = userData[@"isGuest"];
            if (![isguest isEqualToString:@"1"]) {
                [defaults setObject:userData?:@{} forKey:keyStr];
                [defaults synchronize];
            }
        }
        return @{};
    }else{
        NSDictionary *userDic = [defaults dictionaryForKey:keyStr];
        return userDic?:@{};
    }
}

#endif

@end
